package com.myzl.coal.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.myzl.coal.domain.HistoryData;
import com.myzl.coal.mapper.HistoryDataMapper;
import com.myzl.coal.mongo.component.MongoComponent;
import com.myzl.coal.service.IHistoryDataService;
import com.myzl.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 实时数据Service业务层处理
 *
 * @author myzl
 * @date 2022-10-31
 */
@Slf4j(topic="admin-user")
@Service
public class HistoryDataServiceImpl extends ServiceImpl<HistoryDataMapper, HistoryData> implements IHistoryDataService
{
    @Autowired
    private HistoryDataMapper historyDataMapper;
    @Autowired
    private MongoComponent mongoComponent;

    /**
     * 查询实时数据
     *
     * @param id 实时数据主键
     * @return 实时数据
     */
    @Override
    public HistoryData selectHistoryDataById(Long id)
    {
        return historyDataMapper.selectHistoryDataById(id);
    }

    /**
     * 查询实时数据列表
     *
     * @param historyData 实时数据
     * @return 实时数据
     */
    @Override
    public List<HistoryData> selectHistoryDataList(HistoryData historyData)
    {
        return historyDataMapper.selectHistoryDataList(historyData);
    }

    /**
     * 新增实时数据
     *
     * @param historyData 实时数据
     * @return 结果
     */
    @Override
    public int insertHistoryData(HistoryData historyData)
    {
        historyData.setCreateTime(DateUtils.getNowDate());
        return historyDataMapper.insertHistoryData(historyData);
    }

    /**
     * 修改实时数据
     *
     * @param historyData 实时数据
     * @return 结果
     */
    @Override
    public int updateHistoryData(HistoryData historyData)
    {
        historyData.setUpdateTime(DateUtils.getNowDate());
        return historyDataMapper.updateHistoryData(historyData);
    }

    /**
     * 批量删除实时数据
     *
     * @param ids 需要删除的实时数据主键
     * @return 结果
     */
    @Override
    public int deleteHistoryDataByIds(Long[] ids)
    {
        return historyDataMapper.deleteHistoryDataByIds(ids);
    }

    /**
     * 删除实时数据信息
     *
     * @param id 实时数据主键
     * @return 结果
     */
    @Override
    public int deleteHistoryDataById(Long id)
    {
        return historyDataMapper.deleteHistoryDataById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean copyMysqlToMongo(Integer pageSize) {
        List<HistoryData> dataList = historyDataMapper.selectList(new LambdaUpdateWrapper<HistoryData>()
                .orderByAsc(HistoryData::getId).last("limit " + 0 + "," +pageSize));
        if (dataList == null || dataList.isEmpty()){
            return false;
        }
        List<com.myzl.coal.mongo.domain.HistoryData> mongoDataList = dataList.stream().map(historyData -> {
            com.myzl.coal.mongo.domain.HistoryData mongoHistoryData = new com.myzl.coal.mongo.domain.HistoryData();
            BeanUtils.copyProperties(historyData,mongoHistoryData);
            return mongoHistoryData;
        }).collect(Collectors.toList());
        mongoComponent.saveOrUpdateBatch(mongoDataList,"HistoryData");
        List<String> ids = dataList.stream().map(HistoryData::getId).collect(Collectors.toList());
        historyDataMapper.delete(new LambdaUpdateWrapper<HistoryData>().in(HistoryData::getId,ids));
        return true;
    }
}
